import { defineStore } from 'pinia'

export const useTagStore = defineStore('tag', {
  state() {
    return {
      tags: [] as Tag[]
    }
  },
  getters: {},
  actions: {
    InitTags() {
      this.tags = getSystemTags()
      this.PersistTags()
    },
    AddTag(tag: Tag) {
      const exists = this.tags.some((item) => item.path === tag.path)
      if (exists) return
      this.tags.push(tag)
      this.PersistTags()
    },
    RemoveTag(path: string) {
      const index = this.tags.findIndex((item) => item.path === path)
      this.tags.splice(index, 1)
      this.PersistTags()
    },
    CloseOtherTags(path: string) {
      const curTag = this.tags.find((ele) => ele.path === path)!
      this.tags = curTag.path == HomeTag.path ? [HomeTag] : [HomeTag, curTag]
      this.PersistTags()
    },
    PersistTags() {
      setSystemTags(this.tags)
    }
  }
})
